﻿/*-----------------------------------------------------------------------
 * <copyright file="ScriptTypeSelChildWin.xaml.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Zhang jun</author>
 * <summary>
 *          ScriptType(帐票种类) 查询信息子窗体
 *
 *     引用服务：WCFServers.WCFCommonQueryRef
 *     服务功能：帐票种类 查询信息
 *   
 *     引用服务：WCFLog4Ref.WCFLog4Client
 *     服务功能：记录日志
 *
 *   子窗体返回查询用列表值： 
 *          ClientDataList.SelScriptTypeList（记录所选择的 ScriptType 信息, 没加逗号 --全局--）
 *   子窗体返回显示用值：
 *          retRadioButtonContent
 *
 *          作成日： 07/05/2011   
 * </summary>
 *----------------------------------------------------------------------- 
 */
namespace Orid.Management.Statistics.OperatorUnitMangement.Common.Window
{
    using System;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.WCFCommonQueryRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;


    public partial class ScriptTypeSelChildWin : ChildWindow
    {

        /// <summary>
        /// 消息子窗口
        /// messbox
        /// </summary>
        private childWindow.MessageBox errorMessbox = null;

        /// <summary>
        /// 服务引用 (查询ScriptType(帐票种类)信息用)
        /// wcfClient
        /// </summary>
        private WCFCommonQueryClient wcfClient = null;

        /// <summary>
        /// 日志服务定义
        /// wcfLogClient
        /// </summary>
        private WCFLog4Ref.WCFLog4Client wcfLogClient = null;

        /// <summary>
        /// 子窗体返回 显示 用值
        /// </summary>
        public StringBuilder retRadioButtonContent = null;



        public ScriptTypeSelChildWin()
        {
            InitializeComponent();

            // 引用主题
            ((App)Application.Current).SetTheme((Grid)LayoutRoot);

            this.InitField();


            this.Loaded +=new RoutedEventHandler(ScriptTypeSelChildWin_Loaded);

            //清空 ScriptType 信息记录表
            ClientDataList.SelScriptTypeList.Clear();

            //查询ScriptType信息 服务
            this.wcfClient.SelectScriptTypeInforCompleted+=new EventHandler<SelectScriptTypeInforCompletedEventArgs>(wcfClient_SelectScriptTypeInforCompleted);
            
            //选择所有ScriptType 
            this.cbAllScriptType.Click += new RoutedEventHandler(cbAllScriptType_Click);

            this.UnitWrapping.Width = this.Width;
        }


        #region 选择所有ScriptType
        /// <summary>
        /// 选择所有ScriptType 
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The  instance containing the event data.</param>
        void cbAllScriptType_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CheckBox cb = sender as CheckBox;

                ClientDataList.SelScriptTypeList.Clear();

                if (retRadioButtonContent.Length > 0)
                {
                    retRadioButtonContent.Remove(0, retRadioButtonContent.Length);
                }

                //选择所有的ScriptType
                if (cb.IsChecked == true)
                {
                    if (this.UnitWrapping != null)
                    {
                        IsCheckBoxSelect(true, false);
                    }
                }
                else //不选所有ScriptType
                {
                    if (this.UnitWrapping != null)
                    {
                        IsCheckBoxSelect(false, true);
                    }
                }
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " ScriptType选择子窗体 cbAllScriptType_Click方法： " + ex.Message.ToString());
                return;
            }
        }
        #endregion 选择所有ScriptType


        #region 查询ScriptType信息 服务实现
        /// <summary>
        /// 查询ScriptType信息 服务实现
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The  instance containing the event data.</param>
        void wcfClient_SelectScriptTypeInforCompleted(object sender, SelectScriptTypeInforCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    if (e.Result.Count > 0)
                    {
                        //先用RadioButton，为以后的多选(CheckBox)改修改做准备

                        // ******RadioButton 改为 CheckBox*****08/05/2010*****//
                        // 动态生成CheckBox，并给CONTEXT赋值（ScriptType名）
                        this.UnitWrapping.Children.Clear();

                        for (int i = 0; i < e.Result.Count; i++)
                        {
                            CheckBox cb = new CheckBox();

                            //显示值 Content = VALUE
                            cb.Content = e.Result[i].LineValue.ToString();

                            //查询用值 Tag =NAME
                            cb.Tag = e.Result[i].LineName.ToString();

                            cb.IsChecked = false;
                            cb.Margin = new Thickness(10, 3, 2, 5);
                            this.UnitWrapping.Children.Add(cb);
                        }
                        // *****************************************

                        //****选择所有ScriptType 控件*****
                        if (this.UnitWrapping != null && this.UnitWrapping.Children.Count > 0)
                        {
                            this.cbAllScriptType.Visibility = Visibility.Visible;
                            //初始状态，所有ScriptType不选
                            this.cbAllScriptType.IsChecked = false;
                        }
                        // *****************************************
                    }
                    else
                    {
                        throw new Exception("ScriptType列表查询值为空！");
                    }
                }
                else
                {
                    throw new Exception("取服务端ScriptType信息出错！" + e.Error.Message.ToString());
                }
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " ScriptType 查询子窗体 wcfClient_SelectScriptTypeInforCompleted方法： " + ex.Message.ToString());

                Common.FunctionList.FunctionClass.ShowMessageBox(
                   this.errorMessbox,
                   1,
                   "提示",
                   ex.Message.ToString(),
                   Colors.Gray,
                   true,
                   Visibility.Visible,
                   Visibility.Collapsed);
                return;
            }
        }

        #endregion 查询ScriptType信息 服务实现


        #region 变量初始化方法  InitField
        /// <summary>
        /// 变量初始化方法  InitField
        /// </summary>
        private void InitField()
        {
            this.errorMessbox = new childWindow.MessageBox();
            this.wcfClient = Wcf.Service.CommonQuery;
            this.wcfLogClient = Wcf.Service.Log4;
            this.retRadioButtonContent = new StringBuilder();
        }
        #endregion 变量初始化方法  InitField


        #region 页面载入
        /// <summary>
        /// 页面载入
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The  instance containing the event data.</param>
        void ScriptTypeSelChildWin_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                IsCheckBoxSelect(false, true);
                //执行 查询ScriptType信息 服务
                this.wcfClient.SelectScriptTypeInforAsync();
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " ScriptType选择子窗体 ScriptTypeSelChildWin_Loaded方法： " + ex.Message.ToString());
                return;
            }
        }
        #endregion 页面载入


        #region 确定按钮
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The instance containing the event data.</param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // ********RadioButton改为CheckBox******08/05/2010 edit**********
                // 判断哪个CheckBox是选中状态，并将值
                // 传给ClientDataList.SelScriptTypeList(查询用)
                //RetRadioButtonContent(显示值用)

                CheckBox radioButton;

                if (retRadioButtonContent.Length > 0)
                {
                    retRadioButtonContent.Remove(0, retRadioButtonContent.Length);
                }

                ClientDataList.SelScriptTypeList.Clear();

                for (int i = 0; i < this.UnitWrapping.Children.Count; i++)
                {
                    radioButton = this.UnitWrapping.Children[i] as CheckBox;

                    // 如果类型不一致则返回null
                    if (radioButton != null && radioButton.IsChecked == true)
                    {
                        // 不用加引号和逗号(查询用)
                        ClientDataList.SelScriptTypeList.Add(radioButton.Tag.ToString().Trim());

                        // 显示用
                        retRadioButtonContent.Append(radioButton.Content.ToString().Trim()+" ");
                    }
                }
                //如果全选，就显示 AllScriptType
                if (this.cbAllScriptType.IsChecked == true)
                {
                    if (retRadioButtonContent.Length > 0)
                    {
                        retRadioButtonContent.Remove(0, retRadioButtonContent.Length);
                    }
                    retRadioButtonContent.Append(this.cbAllScriptType.Tag.ToString().Trim());
                }

                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + " ScriptType 查询子窗体 OKButton_Click方法：" + ex.Message.ToString());
                return;
            }
        }

        #endregion 确定按钮


        #region  取消 按钮
        /// <summary>
        /// 取消 按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            //清空 ScriptType 信息记录表
            ClientDataList.SelScriptTypeList.Clear();
            if (retRadioButtonContent.Length > 0)
            {
                retRadioButtonContent.Remove(0, retRadioButtonContent.Length);
            }

            this.DialogResult = false;
        }
        #endregion  取消 按钮


        #region Wrapping控件里的CHECK控件的状态设置
        /// <summary>
        /// Wrapping控件里的CHECK控件的状态设置
        /// </summary>
        /// <param name="isChecked">是否全选择</param>
        /// <param name="isEnable">是否可用</param>
        private void IsCheckBoxSelect(bool isChecked, bool isEnable)
        {
            if (this.UnitWrapping == null ||
                    this.UnitWrapping.Children.Count < 0)
            {
                return;
            }

            for (int i = 0; i < this.UnitWrapping.Children.Count; i++)
            {
                CheckBox radioButton = this.UnitWrapping.Children[i] as CheckBox;
                radioButton.IsEnabled = isEnable;
                radioButton.IsChecked = isChecked;
            }
        }

        #endregion Wrapping控件里的CHECK控件的状态设置

    }
}

